function myPromise(excutor) {
    let self = this;
    this.status = "pending";
    this.onResolvedList = [];
    this.onRrejectedList = [];
    this.reson = "";
    this.error = "";

    function resolve(val) {
        if (self.status === "pending") {
            setTimeout(() => {   // 处理同步中,链式 then之后再执行
                self.status = "resolved";
                self.reason = val;
                self.onResolvedList.forEach(fn => fn());
            })

        }
    }

    function reject(err) {
        if (self.status === "pending") {
            setTimeout(() => {
                self.status = "rejected";
                self.error = err;
                self.onRrejectedList.forEach(fn => fn());
            })
        }
    }

    try {
        excutor(resolve, reject)
    } catch (error) {
        reject(error)
    }
}

myPromise.prototype.then = function (infullfilled, inrejectd) {
    infullfilled = typeof infullfilled == "function" ? infullfilled : (val) => val;
    inrejectd = typeof inrejectd === "function" ? inrejectd : function (err) { return err }
    let self = this;
    let promise = new myPromise((resolve, reject) => {
        if (self.status == "pending") {
            self.onResolvedList.push(function () {
                setTimeout(() => {
                    // 如果异步这里reson 与 error 都没值 所以要用settimedout 延迟执行
                    let x = infullfilled(self.reason)
                    // 有可能then 返回的是一个promise ,并且链式调用需要
                    promiseResolve(promise, x, resolve, reject)
                })
            })
            self.onRrejectedList.push(function () {
                setTimeout(() => {
                    let x = inrejectd(self.error);
                    promiseResolve(promise, x, resolve, reject)
                })
            })
        }

        if (self.status == "resolved") {
            setTimeout(() => {
                infullfilled(self.reason)
            });
        }

        if (self.status == "rejected") {
            setTimeout(() => {
                inrejectd(self.error)
            })
        }
    })
    return promise;
}


function promiseResolve(promise, x, resolve, reject) {
    // 判断没有调用自己
    if (x === promise && x != undefined) {
        throw TypeError("")
    }
    if (x !== null && (typeof x == "function" || typeof x == "object")) {
        try {
            // 有可能还是一个promise
            let then = x.then
            if (typeof then == "function") {
                then.call(x, function (y) {
                    resolvePromise(promise, y, resolve, reject)
                }, function (err) {
                    reject(err)
                })
            } else {
                resolve(x)
            }

        } catch (error) {
            reject(error)
        }
    } else {
        resolve(x)
    }

}

new myPromise((resolved, rejectd) => {
    console.log(1)
    // setTimeout(() => {
    resolved(2)
    // }, 0)
}).then((res) => {
    console.log(res)
    console.log("ssss")
    return "s"
}).then((res) => {
    console.log(res)
})

// new Promise((resolved, rejectd) => {
//     console.log(1)
//     // setTimeout(() => {
//         resolved(2)
//     // }, 0)
// }).then((res) => {
//     console.log(res)
//     console.log("ssss")
//     return "s"
// }).then((res) => {
//     console.log(res)
// })
// console.log("=================")

myPromise.prototype.all = function (fns) {
    return new myPromise((resolve, reject) => {
        let result = [];
        for (let i = 0; i < fns.length; i++) {
            fn[i].then(function (res) {
                result[i] = res;
                if (result.length === fns.length) {
                    resolve(result)
                }
            }, function (err) {
                reject(err)
            })
        }

    })
};
myPromise.prototype.race = function (fns) {
    return new myPromise((resolve, reject) => {
        for (let i = 0; i < fns.length; i++) {
            fn[i].then(function (res) {
                resolve(res)
            }, function (err) {
                reject(err)
            })
        }
    })
}